/**
 * 使用Proxy深拷贝对象
 * @param obj 需要复制的对象
 * @param cache 缓存信息，不需要传递
 * @returns 返回深拷贝的对象
 */
export function deepCopy<T extends Record<any, any> = Record<any, any>>(
  obj: T,
  cache = new WeakMap(),
): T {
  if (cache.has(obj)) {
    return cache.get(obj);
  }
  const proxyObj = new Proxy(obj, {
    get(target, key, receiver) {
      const v = Reflect.get(target, key, receiver);
      if (typeof v !== 'object') {
        return v;
      }
      return deepCopy(v, cache);
    },
  });
  cache.set(obj, proxyObj);
  return proxyObj;
}
